using UnityEngine;
using UnityEngine.UI;

namespace CGF
{
    public class ChatListCell : MonoBehaviour, IScrollViewCell
    {
        public RectTransform me, ai;
        public Text myText, aiText;
        public GameObject loadingIcon;

        private float TextMargin = 40;
        private float AvatarHeight = 70;
        private HorizontalLayoutGroup myLayoutGroup, aiLayoutGroup;
        private string senderId;

        public void SetContent(string senderId, string content, bool isLoading)
        {
            this.senderId = senderId;
            bool isSingleLine = TextWidth(content) < 500;
            if (this.senderId == UserInfo.UserId)
            {
                if (!me.gameObject.activeSelf) me.gameObject.SetActive(true);
                if (ai.gameObject.activeSelf) ai.gameObject.SetActive(false);
                if (!myLayoutGroup) myLayoutGroup = me.GetComponent<HorizontalLayoutGroup>();
                if (isSingleLine)
                {
                    myLayoutGroup.childControlWidth = true;
                }
                else
                {
                    myLayoutGroup.childControlWidth = false;
                    Vector2 size = myText.rectTransform.sizeDelta;
                    size.x = 500;
                    myText.rectTransform.sizeDelta = size;
                }

                myText.text = content;
                ChildOffset(me);
            }
            else
            {
                if (me.gameObject.activeSelf) me.gameObject.SetActive(false);
                if (!ai.gameObject.activeSelf) ai.gameObject.SetActive(true);

                if (!aiLayoutGroup) aiLayoutGroup = ai.GetComponent<HorizontalLayoutGroup>();

                if (isLoading)
                {
                    if (!loadingIcon.activeSelf) loadingIcon.SetActive(true);
                    aiText.text = "";
                    //aiLayoutGroup.padding = new RectOffset(50, 50, 40, 50);
                    return;
                }
                else
                {
                    if (loadingIcon.activeSelf) loadingIcon.SetActive(false);
                    //aiLayoutGroup.padding = new RectOffset(50, 50, 40, 80);
                }

                if (isSingleLine)
                {
                    aiLayoutGroup.childControlWidth = true;
                }
                else
                {
                    aiLayoutGroup.childControlWidth = false;
                    Vector2 size = aiText.rectTransform.sizeDelta;
                    size.x = 500;
                    aiText.rectTransform.sizeDelta = size;
                }

                if (content.Length < 12)
                {
                    //字符太短，空间不够防止底部选项，往末尾加空格
                    int addTextCount = 12 - content.Length;
                    for (int i = 0; i < addTextCount; i++)
                    {
                        content += "   ";
                    }
                }

                aiText.text = Utils.RemoveMarkdown(content);
                ChildOffset(ai);
            }
        }
        
        private void ChildOffset(RectTransform child)
        {
            //调用内容包括头部整体垂直居中
            float boxHeight = TextHeight() +  TextMargin * 2; //上下边距
            float totalHeight = boxHeight + AvatarHeight;
            Vector2 pos = child.anchoredPosition;
            pos.y = -(totalHeight * 0.5f - boxHeight * 0.5f);
            child.anchoredPosition = pos;
        }

        public float GetCellSize()
        {
            float cellHeight = TextHeight() + TextMargin * 2 + AvatarHeight;
            (transform as RectTransform).sizeDelta = new Vector2(670, cellHeight);
            return cellHeight;
        }
        
        private float TextHeight()
        {
            Text text = senderId == UserInfo.UserId ? myText : aiText;
            return TextHelper.GetTextHeight(text);
        }
        private float TextWidth(string content)
        {
            Text text = senderId == UserInfo.UserId ? myText : aiText;
            text.text = content;
            return TextHelper.GetTextWidth(text);
        }
    }
}